import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('你好flutter1')),
        body: Column(
          children: [
            const ListTile(
              title: Text('StatelessWidget 无状态组件'),
            ),
            StatelessWidgetApp(),
            const ListTile(
              title: Text('StatefulWidget 有状态组件'),
            ),
            StatefulWidgetApp(),
          ],
        ),
        // TODO Scaffold组件 底部浮动按钮
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          child: const Icon(Icons.add),
        ),
      ),
    ),
  );
}

// TODO 无状态组件  不能动态改变页面的值
// ignore: must_be_immutable
class StatelessWidgetApp extends StatelessWidget {
  int countNum = 0;
  StatelessWidgetApp({super.key});
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Text(
            "$countNum",
            style: const TextStyle(fontSize: 50),
          ),
          const SizedBox(height: 30),
          ElevatedButton(
            onPressed: () {
              countNum++;
              print(countNum);
            },
            child: const Text('增加'),
          )
        ],
      ),
    );
  }
}

// TODO 有状态组件
class StatefulWidgetApp extends StatefulWidget {
  const StatefulWidgetApp({super.key});

  @override
  State<StatefulWidgetApp> createState() => _StatefulWidgetAppState();
}

class _StatefulWidgetAppState extends State<StatefulWidgetApp> {
  int countNum = 0;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Text(
            "$countNum",
            style: const TextStyle(fontSize: 50)
          ),
          const SizedBox(height: 30),
          ElevatedButton(
            onPressed: () {
              // TODO setState只能在StatefulWidget组件内部使用
              setState(() {
                countNum++;
              });
              print(countNum);
            },
            child: const Text('增加'),
          )
        ],
      ),
    );
  }
}
